﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using AutoMapper;
using System.Text;


namespace WaiGaoQiaoWeb.Controllers
{
    public class PersonPostUnitController : Controller
    {
        Work_Person_Unit_PostInfo work = new Work_Person_Unit_PostInfo(true);
        //
        // GET: /Post/

        Work_ArticleBringInfo br = new Work_ArticleBringInfo();
        Work_ArticleInfo article = new Work_ArticleInfo();
        Work_ArticleUnit unit = new Work_ArticleUnit();
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 返回用户姓名
        /// </summary>
        /// <param name="personNo"></param>
        /// <returns></returns>
        public JsonResult RtnPersonName(string personNo)
        {
            Work_ArticleBringInfo bring = new Work_ArticleBringInfo();
            IList<SelectListItem> sl=GetQueryTime(personNo);
            if (sl != null && sl.Count > 0)
            {
                foreach (SelectListItem sli in sl)
                {
                    bring.Article_Unit += sli.Text + ",";
                    bring.clu += sli.Value + ",";
                }
            }
            bring.PersonName = br.RtnPersonName(personNo);
            bring.postid = br.RtnPostNoForPersonNo(personNo);
            return Json(bring);
        }
        /// <summary>
        /// 获取规格列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetUnitList(int? id)
        {
            if (!id.HasValue)
            {
                IDictionary<int, string> dic = new Dictionary<int, string>();
                dic.Add(0, "请选择...");
                IList<SelectListItem> selectList =
                 dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
                return selectList;
            }
            else
            {
                Work_ArticleInfo idinfo = article.GetCon_ArticleById(Convert.ToInt32(id));
                if (!string.IsNullOrEmpty(idinfo.ArticleUnit))
                {
                    idinfo.ArticleUnitId = idinfo.ArticleUnit;
                    idinfo.ArticleUnit = RtnStringUnit(idinfo.ArticleUnit);
                }
                var charUnit = idinfo.ArticleUnit.Split(',');
                var charUnitId = idinfo.ArticleUnitId.Split(',');
                IDictionary<int, string> dic = new Dictionary<int, string>();
                dic.Add(0, "请选择...");
                for (int i = 0; i < charUnitId.Length; i++)
                {
                    dic.Add(Convert.ToInt32(charUnitId[i]), charUnit[i]);
                }
                IList<SelectListItem> selectList =
                    dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
                return selectList;
            }
        }
        /// <summary>
        /// 返回物资规格
        /// </summary>
        /// <param name="strUnit"></param>
        /// <returns></returns>
        public string RtnStringUnit(string strUnit)
        {
            StringBuilder rtnString = new StringBuilder();
            if (!string.IsNullOrEmpty(strUnit))
            {
                if (strUnit.IndexOf(',') > 0)
                {
                    string[] charUnit = strUnit.Split(',');
                    for (int i = 0; i < charUnit.Length; i++)
                    {
                        rtnString.Append(unit.GetCon_ArticleUnitById(Convert.ToInt32(charUnit[i])).Unit + ",");

                    }
                }
                else
                {
                    rtnString.Append(unit.GetCon_ArticleUnitById(Convert.ToInt32(strUnit)).Unit);
                }
            }
            return rtnString.ToString();
        }
        /// <summary>
        /// 获取物品列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetQueryTime(string personNo)
        {
            SelectListItem sl = new SelectListItem();
            sl.Selected = true;
            sl.Text = "请选择...";
            sl.Value = "0";
            if (!string.IsNullOrEmpty(personNo))
            {     //priorID!=0
                IList<Work_ArticleInfo> list = article.GetAllCon_Article(" AND    id in (select ArticleId from dbo.Work_Post_Relation where postid in (select 岗位编号 from dbo.FILE_PERSONNEL_VIEW where personnelid='" + personNo + "'))");
                IList<SelectListItem> selectList =
                 list.Select(m => new SelectListItem() { Text = m.ArticleName.ToString(), Value = m.ID.ToString() }).ToList();
                selectList.Insert(0, sl);
                return selectList;
            }
            else
            {
                IList<SelectListItem> selectList = new List<SelectListItem>();
                selectList.Add(sl);
                return selectList;
            }
        }
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetPostList(PageVM pvm)
        {
            string condition = pvm.query;

            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = work.GetWork_PostCount(condition);
            IList<Work_Person_Unit_PostInfo> list = work.GetAllPost(condition, pvm.rp, pvm.page);
            List<string> v;
            foreach (Work_Person_Unit_PostInfo ida in list)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.PersonNo);
                v.Add(ida.PersonName);
                v.Add(article.GetCon_ArticleById(ida.ArticleId).ArticleName);
                v.Add(unit.GetCon_ArticleUnitById(ida.RuleId).Unit);

                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }
        /// <summary>
        /// 转到添加界面或者修改界面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddPost(int? id)
        {
            PostUnitVM idvm = new PostUnitVM();
        

            if (!id.HasValue)
            {
                idvm.ID = 0;
                ViewBag.UnitList = GetUnitList(null);
                ViewBag.AID = GetQueryTime(null);
            }
            else
            {

                Work_Person_Unit_PostInfo idinfo = work.GetAllPost(Convert.ToInt32(id));
                ViewBag.UnitList = GetUnitList(idinfo.ArticleId);
                ViewBag.AID = GetQueryTime(br.RtnPersonNo(idinfo.PersonNo));
                Mapper.DynamicMap(idinfo, idvm);

            }
            return View(idvm);
        }
        /// <summary>
        /// 删除物品领用规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(int id)
        {
            return Json(work.DeletePost(id));
        }
        /// <summary>
        /// 保存物品领用规则
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveWork_Post(PostUnitVM vm)
        {
            if (ModelState.IsValid)
            {
                Work_Person_Unit_PostInfo info = new Work_Person_Unit_PostInfo();
                Mapper.DynamicMap(vm, info);
                work.SaveWork_Post(info);
                return Json(true);
            }
            else
            {
                return Json(false);
            }


        }
        /// <summary>
        /// 验证是否领取了这个规格
        /// </summary>
        /// <param name="PersonNo"></param>
        /// <param name="ArticleId"></param>
        /// <param name="RuleId"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ValidateExists(string PersonNo, int ArticleId, int RuleId)
        {
            Work_Person_Unit_PostInfo info = work.GetWork_Person_Unit_PostInfo(" AND PersonNo='" + br.RtnPidForPersonNo(PersonNo) + "' AND ArticleId='" + ArticleId + "' AND RuleId='" + RuleId + "'");
            if (info != null)
                return Json(false);
            else
                return Json(true);
        }

    }
}
